﻿using RTW.Core.Extensions;
using RTW.Core.Ioc;
using RTW.Core.Repository;
using RTW.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RTW.Servers
{
    public class PersonalSuSongServers : BaseServers<PersonalSuSongFiles>, IAutoInject
    {
        private readonly IRepositoryAsync<PersonalSuSongFiles> _personalSuSongFilesRepository;
        private readonly IRepositoryAsync<PersonalSuSongCover> _personalSuSongCoverRepository;
        private readonly IRepositoryAsync<PersonalSuSongDirectory> _personalSuSongDirectoryRepository;
        private readonly IRepositoryAsync<PersonalSuSongPages> _personalSuSongPagesRepository;
        private readonly IRepositoryAsync<PersonalSuSongFilesList> _personalSuSongFilesListRepository;

        public PersonalSuSongServers()
        {
            _personalSuSongFilesRepository = new DapperAsyncRepository<PersonalSuSongFiles>();
            _personalSuSongCoverRepository = new DapperAsyncRepository<PersonalSuSongCover>();
            _personalSuSongDirectoryRepository = new DapperAsyncRepository<PersonalSuSongDirectory>();
            _personalSuSongPagesRepository = new DapperAsyncRepository<PersonalSuSongPages>();
            _personalSuSongFilesListRepository = new DapperAsyncRepository<PersonalSuSongFilesList>();
        }

        /// <summary>
        ///获取全部的个人诉讼数据条数
        /// </summary>
        /// <param name="dwid">单位id</param>
        /// <param name="fileState">案卷状态</param>
        /// <param name="fileClass">案卷类型</param>
        /// <param name="fileNo">档号</param>
        /// <param name="fYear">年度</param>
        /// <param name="fTerm">期限</param>
        /// <param name="caseValue">案由</param>
        /// <param name="dsr">当事人</param>
        /// <returns></returns>
        public async Task<int> GetAllPersonalSuSongFilesTotal(PagingParameter selectParameter, string dwid, string fileState, string fileClass, string fileNo, string fYear, string fTerm, string caseValue, string dsr)
        {
            //根据参数拼接查询语句
            string sql = string.Format("select count(1) from Personal_SuSong_Files as a left join Personal_SuSong_Cover as b on a.CoverID = b.CoverID ");
            sql += string.Format(" where a.DWID='{0}'", dwid);
            if (!string.IsNullOrWhiteSpace(fileState))
            {
                sql += string.Format(" and a.FilesState='{0}' ", fileState);
            }
            if (!string.IsNullOrWhiteSpace(fileClass))
            {
                sql += string.Format(" and a.FType='{0}' ", fileClass);
            }
            if (!string.IsNullOrWhiteSpace(fileNo))
            {
                sql += string.Format(" and a.FileNo like '%{0}%' ", fileNo);
            }
            if (!string.IsNullOrWhiteSpace(fYear))
            {
                string fyearStar = fYear.Split('-')[0];
                string fYearEnd = fYear.Split('-')[1];
                sql += string.Format(" and a.FYear>='{0}' and FYear<='{1}' ", fyearStar, fYearEnd);
            }
            if (!string.IsNullOrWhiteSpace(fTerm))
            {
                sql += string.Format(" and a.FTermCode='{0}' ", fTerm);
            }
            if (!string.IsNullOrWhiteSpace(caseValue))
            {
                sql += string.Format(" and b.CaseCause like '%{0}%' ", caseValue);
            }
            if (!string.IsNullOrWhiteSpace(dsr))
            {
                sql += string.Format(" and b.Litigant like '%{0}%' ", dsr);
            }

            if (!string.IsNullOrWhiteSpace(selectParameter.field) && !string.IsNullOrWhiteSpace(selectParameter.field))
            {
                sql += string.Format(" order by {0} {1} ", selectParameter.field, selectParameter.order);
            }
            else
            {
                sql += "order by a.fYear asc,a.fTerm asc";
            }

            var ssFiles = await _personalSuSongFilesRepository.SqlWithParams(sql);
            int total = Convert.ToInt32(ssFiles);
            return total;
        }


        /// <summary>
        /// 分页查询获取全部的个人诉讼数据
        /// </summary>
        /// <param name="selectParameter">分页参数</param>
        /// <param name="dwid">单位id</param>
        /// <param name="fileState">案卷状态</param>
        /// <param name="fileClass">案卷类型</param>
        /// <param name="fileNo">档号</param>
        /// <param name="fYear">年度</param>
        /// <param name="fTerm">期限</param>
        /// <param name="caseValue">案由</param>
        /// <param name="dsr">当事人</param>
        /// <returns></returns>
        public async Task<GetData> GetAllPersonalSuSongFiles(PagingParameter selectParameter, string dwid, string fileState, string fileClass, string fileNo, string fYear, string fTerm, string caseValue, string dsr)
        {
            GetData getData = new GetData();

           // PersonalSuSongFilesList personalSuSongFilesList = new PersonalSuSongFilesList();

            // 分页查询语法 select * from "JCYDB"."Personal_SuSong_Files" order by id desc limit 1,10    limit 后第一个参数为index，第二个参数为条数

            //根据参数拼接查询语句
            string tabcol = " a.FilesState,a.NewOrOld,a.id,a.fileNo,a.fType,a.fYear,a.fTerm,a.bookType,a.dept,b.CaseCause,b.litigant,b.ssr,b.bssr,b.fzxyrbgr,b.sqr,b.otherLitigant,b.unDepart,b.undertaker,b.danWei,b.receiveDate,b.endDate,b.result1,b.result2,b.filingDate,b.pageCount,b.secure ";
            string sql = string.Format("select {0} from Personal_SuSong_Files as a left join Personal_SuSong_Cover as b on a.CoverID = b.CoverID ", tabcol);
            sql += string.Format(" where a.DWID='{0}'",dwid);
            if (!string.IsNullOrWhiteSpace(fileState))
            {
                sql += string.Format(" and a.FilesState='{0}' ",fileState);
            }
            if (!string.IsNullOrWhiteSpace(fileClass))
            {
                sql += string.Format(" and a.FType='{0}' ", fileClass);
            }
            if (!string.IsNullOrWhiteSpace(fileNo))
            {
                sql += string.Format(" and a.FileNo like '%{0}%' ", fileNo);
            }
            if (!string.IsNullOrWhiteSpace(fYear))
            {
                string fyearStar = fYear.Split('-')[0];
                string fYearEnd = fYear.Split('-')[1];
                sql += string.Format(" and a.FYear>='{0}' and FYear<='{1}' ", fyearStar, fYearEnd);
            }
            if (!string.IsNullOrWhiteSpace(fTerm))
            {
                sql += string.Format(" and a.FTermCode='{0}' ", fTerm);
            }
            if (!string.IsNullOrWhiteSpace(caseValue))
            {
                sql += string.Format(" and b.CaseCause like '%{0}%' ", caseValue);
            }
            if (!string.IsNullOrWhiteSpace(dsr))
            {
                sql += string.Format(" and b.Litigant like '%{0}%' ", dsr);
            }

            if (!string.IsNullOrWhiteSpace(selectParameter.field) && !string.IsNullOrWhiteSpace(selectParameter.field))
            {
                sql += string.Format(" order by {0} {1} ", selectParameter.field, selectParameter.order);
            }
            else
            {
                sql += "order by a.fYear asc,a.fTerm asc";
            }

            var ssfiles= await _personalSuSongFilesListRepository.SqlWithParams(sql);

            getData.total = ssfiles.Count();

            sql += string.Format(" limit {0},{1} ", (selectParameter.page - 1)* selectParameter.limit, selectParameter.limit);

            var ssFiles = await _personalSuSongFilesListRepository.SqlWithParams(sql);

            getData.personalSuSongFileList = ssFiles.ToList<PersonalSuSongFilesList>();

            return getData;
        }

        /// <summary>
        /// 根据档案id获取案卷
        /// </summary>
        /// <param name="档案id">id</param>
        /// <returns></returns>
        public async Task<PersonalSuSongFiles> GetPersonalSuSongFilesForId(int id)
        {
            var ssFiles = await _personalSuSongFilesRepository.GetByIdAsync(id);
            return ssFiles;
        }

        /// <summary>
        /// 根据档案id获取封面信息
        /// </summary>
        /// <param name="fileid">档案id</param>
        /// <returns></returns>
        public async Task<PersonalSuSongCover> GetPersonalSuSongCoverForFilesID(string fileid)
        {
            var ssFiles = await _personalSuSongCoverRepository.SqlWithParamsSingle("select * from Personal_SuSong_Cover  where FilesID=:FilesID", new { FilesID = fileid });
            return ssFiles;
        }

        /// <summary>
        /// 添加案卷信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> PersonalSuSongFilesAdd(PersonalSuSongFiles model)
        {
            return await _personalSuSongFilesRepository.InsertAsync(model);
        }

        /// <summary>
        /// 添加封面信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> PersonalSuSongCoverAdd(PersonalSuSongCover model)
        {
            return await _personalSuSongCoverRepository.InsertAsync(model);
        }

        /// <summary>
        /// 更新封面信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> PersonalSuSongCoverEdit(PersonalSuSongCover model)
        {
            return await _personalSuSongCoverRepository.UpdateAsync(model);
        }

        /// <summary>
        /// 删除案卷
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> PersonalSuSongFilesDel(string ids)
        {
            return await _personalSuSongFilesRepository.DeleteByIdsAsync(ids);
        }

        /// <summary>
        /// 根据案卷id获取卷内目录
        /// </summary>
        /// <param name="fileId">案卷id</param>
        /// <returns></returns>
        public async Task<List<PersonalSuSongDirectory>> GetDirectoryForFileId(string fileId)
        {
            return (await _personalSuSongDirectoryRepository.SqlWithParams("select * from Personal_SuSong_Directory  where FilesID=:FilesID order by DirIndex asc", new { FilesID = fileId })).ToList();
        }

        /// <summary>
        /// 删除卷内目录
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> PersonalSuSongDirectoryDel(string ids)
        {
            return await _personalSuSongDirectoryRepository.DeleteByIdsAsync(ids);
        }

        /// <summary>
        /// 添加封面信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<int> PersonalSuSongCoverAdd(PersonalSuSongDirectory model)
        {
            return await _personalSuSongDirectoryRepository.InsertAsync(model);
        }
    }
}